package com.manong.web.controller;

import com.github.pagehelper.PageInfo;
import com.manong.data.ResultData;
import com.manong.domain.Role;
import com.manong.query.QueryObject;
import com.manong.query.RoleQueryObject;
import com.manong.service.IRoleService;
import com.manong.util.JsonResult;
import com.manong.util.RequiredPermission;
import com.manong.vo.RolePermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/role")
@CrossOrigin(allowCredentials = "true")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 分页查询接口
     * @param queryObject
     * @return
     */
    @GetMapping("/list")
    @RequiredPermission(name = "角色列表", expression = "role:list")
    public JsonResult list(RoleQueryObject queryObject) {
        PageInfo pageInfo = roleService.query(queryObject);
        // 先解决JsonResult->Data属性解决
        ResultData data = new ResultData(queryObject.getPageNum(), queryObject.getPageSize(),pageInfo.getList(),Integer.parseInt(pageInfo.getTotal() + ""));
        return JsonResult.success(data);
    }

    /**
     * 删除角色接口
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    @RequiredPermission(name = "角色删除", expression = "role:delete")
    public JsonResult delete(@PathVariable Long id) {
        roleService.delete(id);
        return JsonResult.success();
    }

    /**
     * 角色新增或者编辑接口
     * @param rolePermissionVo
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @RequiredPermission(name = "角色新增或者编辑", expression = "role:saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody RolePermissionVo rolePermissionVo) {
        roleService.saveOrUpdate(rolePermissionVo);
        return JsonResult.success();
    }

    /**
     * 查询单个角色
     * @param id
     * @return
     */
    @GetMapping("/getRole/{id}")
    @RequiredPermission(name = "查询单个角色", expression = "role:getRole")
    public JsonResult getRole(@PathVariable Long id) {
        Role role = roleService.get(id);
        // JsonResult data属性 role
        return JsonResult.success(role);
    }

    /**
     * 查询全部角色
     * @return
     */
    @GetMapping("/getRoleList")
    @RequiredPermission(name = "查询全部角色", expression = "role:getRoleList")
    public JsonResult getRoleList() {
        List<Role> all = roleService.getAll();
        return JsonResult.success(all);
    }

    @GetMapping("/getEmployeeRole/{id}")
    @RequiredPermission(name = "查询员工角色", expression = "role:getEmployeeRole")
    public JsonResult getEmployeeRole(@PathVariable Long id) {
        List<Role> rs = roleService.getEmployeeRole(id);
        return JsonResult.success(rs);
    }
}
